<?php
/**
 * 用户消息
 */
class APP_Message extends Data_Base
{
    private $_uid = 0;

    public static function record($toUid, $fromUid, $message, $type, $typeNid, $image, $url)
    {
        $row = array(
            'toUid' => (int) $toUid,
            'fromUid' => (int) $fromUid,
            'message' => $message,
            'type'    => $type,
            'nid'     => (int) $typeNid,
            'image'   => $image,
            'url'     => $url,
            'created' => DB::expr('unix_timestamp(now())'),
        );

        list($insert_id, $rows) = DB::insert('user_v2_message')
            ->columns (array_keys($row))
            ->values(array_values($row))
            ->execute();

        return $insert_id > 0;
    }

    // 给指定用户发送微信通知消息(文字)
    public static function sendUserCustomMessage ($userUid, $textMessage)
    {
        try {
	        include_once MODPATH.'/passport/classes/passport/weixin/wechat.class.php';
	        include_once DOCROOT.'/resource/wxpay/lib/WxPay.Config.php';
	
	        $options = array(
	            'appid' => WxPayConfig::APPID,
	            'appsecret' => WxPayConfig::APPSECRET,
	        );
	
	        $user = User::getUserFromUid($userUid);
	        $userOpenId = $user->getUserOpenId();
	        if (empty($userOpenId))
	            return;
	
	        $message = array(
	            'touser'  => $userOpenId,
	            'msgtype' => 'text',
	            'text'    => array(
	                'content' => $textMessage,
	            ),
	        );
	
	        $wx = new WeChat($options);
	        $wx->sendCustomMessage($message);
        }
        catch (Exception $e)
        {
            //
        }
    }


    public function __construct($uid)
    {
        $this->_uid = (int) $uid;
    }

    public function getList ($offset, $limit)
    {
        $ret = array(
            'total' => 0,
            'list'  => array(),
        );

        if ($this->_uid == 0)
            return $ret;

        $total = self::counts('user_v2_message', array('toUid', '=', $this->_uid));
        $query = DB::select()
            ->from('user_v2_message')
            ->where('toUid', '=', $this->_uid)
            ->order_by('created', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->execute()
            ->as_array();

        foreach ($query as & $rs)
        {
            $fromUid = $rs['fromUid'];

            $rs['createdFormated'] = App_Time::formatDateTime($rs['created']);

            if ($fromUid > 0)
            {
                $rs['fromUserUid']  = $rs['fromUid'];
                $fromUser = User::getUserFromUid($fromUid);
                $rs['fromUserName'] = $fromUser->getUserName();
                $rs['fromUserIcon'] = $fromUser->getUserIcon();
            }
            else
            {
                $rs['fromUserUid']  = 0;
                $rs['fromUserName'] = '体育圈子';
                $rs['fromUserIcon'] = '/resource/skin/img/system_avatar.png';
            }
        }

        $ret['total'] = $total;
        $ret['list']  = $query;

        return $ret;
    }
}
